.<?
include_once('zimbra.mod.inc.phps');

function zimbra_connect() {
	global $cfg;
	
	$za = new zimbraAdmin($cfg['zimbra_server']);
	if ($za->Connect($cfg['zimbra_user'], $cfg['zimbra_password'])) {
		webcp_log(3,0,'zimbra-mod',"Connected to Zimbra server: ".$cfg['zimbra_server'],0,false);
		return $za;
	} else {
		webcp_log(1,0,'zimbra-mod',"Failed to connect to Zimbra server: ".$cfg['zimbra_server'],0,false);
		return false;
	}
}

function zimbra_domain_create(&$data) {
	global $cfg;

 	webcp_log(3,0,'zimbra-mod',"Creating Domain",0,false);

	$za = zimbra_connect();
	if ($za) {
		switch ($data['type']) {
			case 'pointer':
				$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['owner'].'"';
				$res = mysql_query($SQL);
				if (mysql_num_rows($res)) {
					$row = mysql_fetch_array($res);
					$owner = $row['domain'];
					if ($za->AddDomainAlias($owner, $data['domain'])) {
						webcp_log(3,0,'zimbra-mod',"Pointer created ".$data['domain'].' on '.$owner,0,false);
					} else {
						webcp_log(1,0,'zimbra-mod',"Pointer creation failed (pointer ".$data['domain'].' on '.$owner.')',0,false);
					}
				}
				break;
			case 'domain':
				if ($za->CreateDomain($data['domain'])) {
					webcp_log(3,0,'zimbra-mod',"Domain created: ".$data['domain'],0,false);
				} else {
					webcp_log(1,0,'zimbra-mod',"Domain creation failed: ".$data['domain'],0,false);
				}
				break;
			case 'subdomain':
				return;
				break;
		}
	} else {
		webcp_log(1,0,'zimbra-mod',"Domain creation failed: ".$data['domain'],0,false);
	}
}

function zimbra_domain_remove(&$data) {
	global $cfg;

	webcp_log(3,0,'zimbra-mod',"Removing Domain",0,false);
	
	$za = zimbra_connect();
	if ($za) {
		switch ($data['type']) {
			case 'pointer':
				if ($za->DeleteDomain($data['domain'])) {
					webcp_log(3,0,'zimbra-mod','Pointer deleted: '.$data['domain'],0,false);
				} else {
					webcp_log(1,0,'zimbra-mod','Pointer deletion failed: '.$data['domain'],0,false);
				}
				break;
			case 'domain':
				if ($za->DeleteDomain($data['domain'])) {
					webcp_log(3,0,'zimbra-mod','Domain deleted: '.$data['domain'],0,false);
				} else {
					webcp_log(1,0,'zimbra-mod','Domain deletion failed: '.$data['domain'],0,false);
				}
				break;
			case 'subdomain':
				return;
				break;
		}
	} else {
		webcp_log(1,0,'zimbra-mod','Domain deletion failed: '.$data['domain'],0,false);
	}
}

function zimbra_domain_update(&$data) {
	global $cfg;

	webcp_log(3,0,'zimbra-mod',"Updating Domain",0,false);	

	$za = zimbra_connect();
	if ($za) {
		switch ($data['type']) {
			case 'pointer':
				$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['owner'].'"';
				$res = mysql_query($SQL);
				if (mysql_num_rows($res)) {
					$row = mysql_fetch_array($res);
					$owner = $row['domain'];
					
					if (!$za->DomainExists($data['domain1'])) {
						zimbra_domain_create($data);
					}
					
					if ($data['domain1'] != $data['domain']) {
						$za->RemoveDomainAlias($data['domain1']);
						if ($za->AddDomainAlias($owner, $data['domain'])) {
							webcp_log(3,0,'zimbra-mod',"Renamed pointer ".$data['domain1'].' to '.$data['domain'].' on '.$owner,0,false);
						} else {
							webcp_log(1,0,'zimbra-mod',"Pointer rename failed (".$data['domain1'].' to '.$data['domain'].' on '.$owner.')',0,false);
						}
					}
				}
				break;
			case 'domain':
				if (!$za->DomainExists($data['domain1'])) {
					zimbra_domain_create($data);
				}
				//$za->RenameDomain($data['domain'], $data['domain1']);
				break;
			case 'subdomain':
				return;
				break;
		}
	} else {
		webcp_log(3,0,'zimbra-mod',"Domain update failed: ".$data['domain1'],0,false);
	}
}

function zimbra_user_create(&$data) {
	global $cfg;

	webcp_log(3,0,'zimbra-mod',"Creating User",0,false);
	
	$za = zimbra_connect();
	if ($za) {
		if (!isset($data['domain'])) {
			$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['id'].'"';
			$res = mysql_query($SQL);
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$domain = $row['domain'];
			}
		} else {
			$domain = $data['domain'];
		}
		if ($domain) {
			$account = $data['username'].'@'.$domain;
			if ($za->CreateAccount($account, $data['password'])) {
				webcp_log(3,0,'zimbra-mod',"Account created: ".$account,0,false);
			} else {
				webcp_log(1,0,'zimbra-mod',"Account creation failed: ".$account,0,false);
			}
			
			$aliases = explode(' ', $data['aliases']);
			foreach($aliases as $alias) {
				if ($alias) {
					$alias_account = $alias.'@'.$domain;
					if ($za->AddAccountAlias($account, $alias_account)) {
						webcp_log(3,0,'zimbra-mod',"Account alias added: ".$alias_account.' on '.$account,0,false);
					} else {
						webcp_log(1,0,'zimbra-mod',"Account alias add failed: ".$alias_account.' on '.$account,0,false);
					}
				}
			}
		} else {
			webcp_log(1,0,'zimbra-mod',"Account creation failed due to missing owner: ".$data['username'],0,false);
		}
	} else {
		webcp_log(1,0,'zimbra-mod',"Account creation failed: ".$data['username'],0,false);
	}
}

function zimbra_user_remove(&$data) {
	global $cfg;

	webcp_log(3,0,'zimbra-mod',"Removing User",0,false);
	
	$za = zimbra_connect();
	if ($za) {
		if (!isset($data['domain'])) {
			$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['id'].'"';
			$res = mysql_query($SQL);
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$domain = $row['domain'];
			}
		} else {
			$domain = $data['domain'];
		}
		if ($domain) {
			$account = $data['username'].'@'.$domain;
			if ($za->DeleteAccount($account)) {
				webcp_log(3,0,'zimbra-mod',"Account deleted: ".$account,0,false);
			} else {
				webcp_log(1,0,'zimbra-mod',"Account deletion failed: ".$account,0,false);
			}
		} else {
			webcp_log(1,0,print_r($data, true),"Account deletion failed due to missing owner: ".$data['username'],0,false);
		}
	} else {
		webcp_log(1,0,'zimbra-mod',"Account deletion failed: ".$data['username'],0,false);
	}
}

function zimbra_user_suspend(&$data) {
	global $cfg;

	webcp_log(3,0,'zimbra-mod',"Suspending User",0,false);
	
	$za = zimbra_connect();
	if ($za) {
		if (!isset($data['domain'])) {
			$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['id'].'"';
			$res = mysql_query($SQL);
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$domain = $row['domain'];
			}
		} else {
			$domain = $data['domain'];
		}
		if ($domain) {
			$account = $data['username'].'@'.$domain;
			if ($za->SetAccountAttributes($account, array('zimbraAccountStatus' => 'locked'))) {
				webcp_log(3,0,'zimbra-mod',"Account suspended: ".$account,0,false);
			} else {
				webcp_log(1,0,'zimbra-mod',"Account suspension failed: ".$account,0,false);
			} 
		} else {
			webcp_log(1,0,'zimbra-mod',"Account suspension failed due to missing owner: ".$data['username'],0,false);
		}
	} else {
		webcp_log(1,0,'zimbra-mod',"Account suspension failed: ".$data['username'],0,false);
	}
}

function zimbra_user_unsuspend(&$data) {
	global $cfg;

	webcp_log(3,0,'zimbra-mod',"Unsuspending User",0,false);
	
	$za = zimbra_connect();
	if ($za) {
		if (!isset($data['domain'])) {
			$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['id'].'"';
			$res = mysql_query($SQL);
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$domain = $row['domain'];
			}
		} else {
			$domain = $data['domain'];
		}
		if ($domain) {
			$account = $data['username'].'@'.$domain;
			if ($za->SetAccountAttributes($account, array('zimbraAccountStatus' => 'active'))) {
				webcp_log(3,0,'zimbra-mod',"Account unsuspended: ".$account,0,false);
			} else {
				webcp_log(1,0,'zimbra-mod',"Account unsuspension failed: ".$account,0,false);
			}
		} else {
			webcp_log(1,0,'zimbra-mod',"Account unsuspension failed due to missing owner: ".$data['username'],0,false);
		}
	} else {
		webcp_log(1,0,'zimbra-mod',"Account unsuspension failed: ".$data['username'],0,false);
	}
}

function zimbra_user_update(&$data) {
	global $cfg, $echo;

	webcp_log(3,0,'zimbra-mod',"Updating User",0,$echo);
	
	$za = zimbra_connect();
	if ($za !== false) {
		if (!isset($data['domain'])) {
			$SQL = 'SELECT domain FROM domains WHERE id = "'.$data['id'].'"';
			$res = mysql_query($SQL);
			if (mysql_num_rows($res)) {
				$row = mysql_fetch_array($res);
				$domain = $row['domain'];
			}
		} else {
			$domain = $data['domain'];
		}

		if (isset($domain)) {
			$z_account = $data['username'].'@'.$domain;
			webcp_log(3,0,'zimbra-mod',"Updating Account: ".$z_account,0,$echo);

			$exists = $za->AccountExists($z_account);
			webcp_log(3,0,'zimbra-mod',"Checking Existance: ".$z_account,0,$echo);
			
			if ($exists !== true) {
				webcp_log(2,0,'zimbra-mod',"Creating Account: ".$z_account,0,$echo);
				zimbra_user_create($data);
			} else {
				webcp_log(3,0,'zimbra-mod',"Account Exists: ".$z_account,0,$echo);
			}				
			
			// Set password
			if (strlen(trim($data['password']))) {
				if ($za->SetAccountPassword($z_account, $data['password'])) {
					webcp_log(3,0,'zimbra-mod',"Account password updated: ".$z_account,0,$echo);
				} else {
					webcp_log(1,0,'zimbra-mod',"Account password update failed: ".$z_account,var_dump($za, 1),$echo);
				}
			}
			
			// Set forwarding
			if (strlen(trim($data['forward']))) {
				if ($za->SetAccountAttributes($z_account, array('zimbraPrefMailForwardingAddress' => trim($data['forward'])))) {
					webcp_log(3,0,'zimbra-mod',"Account forwarding set: ".$data['forward'].' on '.$z_account,0,$echo);
				} else {
					webcp_log(1,0,'zimbra-mod',"Account forwarding failed: ".$data['forward'].' on '.$z_account,0,$echo);
				}
			}
			
			// Calculate new/old alias arrays
			$new_aliases = explode(' ', trim($data['aliases']));
			foreach($new_aliases as $key => $alias) {
				if (strlen(trim($alias))) {
					$new_aliases[$key] = $alias.'@'.$domain;
				}
			}

			$cur_aliases = $za->GetAccountAliases($z_account);
			$remove_aliases = array_diff($cur_aliases, $new_aliases);
			$add_aliases = array_diff($new_aliases, $cur_aliases);

			// Add missing aliases		
			if (sizeOf($add_aliases)) {
				foreach($add_aliases as $alias) {
					if ($za->AddAccountAlias($z_account, $alias)) {
						webcp_log(3,0,'zimbra-mod',"Account alias added: ".$alias.' on '.$z_account,0,$echo);
					} else {
						webcp_log(1,0,'zimbra-mod',"Account alias add failed: ".$alias.' on '.$z_account,0,$echo);
					}
				}
			}
			
			// Remove old aliases
			if (sizeOf($remove_aliases)) {
				foreach($remove_aliases as $alias) {
					if (strlen(trim($alias))) {
						if ($za->RemoveAccountAlias($z_account, $alias)) {
							webcp_log(3,0,'zimbra-mod',"Account alias removed: ".$alias.' on '.$z_account,0,$echo);
						} else {
							webcp_log(1,0,'zimbra-mod',"Account alias removal failed: ".$alias.' on '.$z_account,0,$echo);
						}
					}
				}
			}
		} else {
			webcp_log(1,0,'zimbra-mod',"Account update failed due to missing owner: ".$data['username'],0,$echo);
		}
	} else {
		webcp_log(1,0,'zimbra-mod',"Account update failed: ".$data['username'],0,$echo);
	}
}

function zimbra_add_lang(&$data) {
	global $T;
	switch($data['lang']) {
		default:
			$T['zimbra service'] = 'Mail Server';
			break;
	}
}

function zimbra_add_cfg(&$cfg) {
	global $cfg;
	if (!isset($cfg['zimbra_server'])) {
		$cfg['zimbra_server'] = '127.0.0.1';
		$cfg['zimbra_user'] = 'admin@';
		$cfg['zimbra_password'] = 'ZimbraPassword';
	}
}

function zimbra_monitor(&$data) {
	global $cfg;
	
	$za = new zimbraAdmin($cfg['zimbra_server']);
	if ($za->Connect($cfg['zimbra_user'], $cfg['zimbra_password'])) {
		$data['zimbra'] = 'up';
	} else {
		$data['zimbra'] = 'down';
	}
	return true;
}

?>
